﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElCharro01.Models;
using System.Text.RegularExpressions;

namespace ElCharro01.Areas.User.Controllers
{
    public class VariantController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /User/Variant/

        public ViewResult Index()
        {
            return View(db.VariantSet.ToList());
        }

        //
        // GET: /User/Variant/Details/5

        public ViewResult Details(int id, string colour = null, string size = null)
        {

            Variant variant = db.VariantSet.Single(v => v.ID == id);


            List<String> SizeVariants = new List<String>();
            List<Variant> SizedVariants;
            List<String> ColourVariants = new List<String>();
            List<String> HexColourVariants = new List<String>();
            List<ElCharro01.Models.Image> imgList;
            ViewBag.IsInStock = true;

            //riempio la lista delle taglie
            if (Regex.IsMatch(variant.Taglia, @"^\d+$"))
            {
                for (int i = 28; i <= 40; i++)
                {
                    string toAdd = i.ToString();
                    SizeVariants.Add(toAdd);
                }
            }

            else
            {
                SizeVariants.Add("S");
                SizeVariants.Add("XS");
                SizeVariants.Add("M");
                SizeVariants.Add("L");
                SizeVariants.Add("XL");
                SizeVariants.Add("XXL");

            }
            if (size != null)
            {
                // se viene selezionata la taglia aggiorno la lista dei colori disponibili
                SizedVariants = new List<Variant>(db.VariantSet.Where(v => v.Product.ID == variant.Product.ID && v.Taglia == size).ToList());

                foreach (Variant var in SizedVariants)
                {
                    ColourVariants.Add(var.Colore);
                    HexColourVariants.Add(var.CodiceColore);
                }

                //passo alla view la variant relativa alla taglia ed al colore selezionato
                if (db.VariantSet.Where(v => v.Product.ID == variant.Product.ID && v.Taglia == size && v.Colore == colour).Count() >= 1)
                {
                    variant = db.VariantSet.Where(v => v.Product.ID == variant.Product.ID && v.Taglia == size && v.Colore == colour).First();
                    ViewBag.IsInStock = true;
                }
                else if (db.VariantSet.Where(v => v.Product.ID == variant.Product.ID && v.Taglia == size).Count() >= 1)
                {
                    variant = db.VariantSet.Where(v => v.Product.ID == variant.Product.ID && v.Taglia == size).First();
                    ViewBag.IsInStock = true;
                }

                else
                {
                    variant = db.VariantSet.Single(v => v.ID == id);
                    ViewBag.IsInStock = false;
                }
            }
            else
            { // la prima volta che accedo alla schermata detail mostrerò i colori relativi alla taglia della variante selezionata clickando sull'immagine
                SizedVariants = new List<Variant>(db.VariantSet.Where(v => v.Product.ID == variant.Product.ID && v.Taglia == variant.Taglia).ToList());
                foreach (Variant var in SizedVariants)
                {
                    ColourVariants.Add(var.Colore);
                    HexColourVariants.Add(var.CodiceColore);
                }
            }


            imgList = variant.Image.ToList();
            imgList.Remove(imgList.First());
            ViewBag.ImgList = imgList;
            ViewBag.ColourVariants = ColourVariants.Distinct();
            ViewBag.SizeVariants = SizeVariants.Distinct();
            ViewBag.HexColourVariants = HexColourVariants.Distinct();
            ViewBag.SizedVariants = SizedVariants.ToList();
            return View(variant);
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}